package org.aplikator.server;

import org.aplikator.server.descriptor.ServerDescriptorBase;

import java.util.HashMap;
import java.util.Map;

public final class DescriptorRegistry {
    private static DescriptorRegistry instance = new DescriptorRegistry();

    private DescriptorRegistry() {
    }

    public static DescriptorRegistry get() {
        return instance;
    }

    private static Map<String, ServerDescriptorBase> descriptionItems = new HashMap<String, ServerDescriptorBase>();

    public synchronized void registerDescriptionItem(ServerDescriptorBase item) {
        if (descriptionItems.containsKey(item.getId())){
            throw new IllegalArgumentException("Description item with ID '"+item.getId()+ "' is already registered");
        }
        descriptionItems.put(item.getId(), item);
    }

    public ServerDescriptorBase getDescriptionItem(String id) {
        ServerDescriptorBase retval = descriptionItems.get(id);
        if (retval == null) {
            throw new IllegalStateException("Could not find DescriptionItem " + id);
        }
        return retval;
    }
    
    public synchronized boolean containsItem(String id) {
    	return descriptionItems.containsKey(id);    	
    }

}
